﻿using SketchUpAPI.NET.Enums;
using SketchUpAPI.NET.SketchUp.Exception;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace SketchUpAPI.NET.Objects
{
    /// <summary>
    /// 阴影设置
    /// </summary>
    /// <remarks>对 SketchUpAPI 中的 SUShadowInfoRef 的包装</remarks>
    public class ShadowInfo : ObjectBase
    {
        #region API 函数
        /// <summary>
        /// 属性个数
        /// </summary>
        /// <param name="shadow_info"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUShadowInfoGetNumKeys(IntPtr shadow_info, out int count);

        /// <summary>
        /// 属性
        /// </summary>
        /// <param name="shadow_info"></param>
        /// <param name="len"></param>
        /// <param name="keys"></param>
        /// <param name="realcount"><see cref="StringRef"/>[]</param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUShadowInfoGetKeys(IntPtr shadow_info, int len, IntPtr[] keys, out int realcount);

        /// <summary>
        /// 获取值
        /// </summary>
        /// <param name="shadow_info"></param>
        /// <param name="key"></param>
        /// <param name="value_out"><see cref="TypedValue"/></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUShadowInfoGetValue(IntPtr shadow_info, [MarshalAs(UnmanagedType.LPUTF8Str)] string key, out IntPtr value_out);

        /// <summary>
        /// 设置值
        /// </summary>
        /// <param name="shadow_info"></param>
        /// <param name="key"></param>
        /// <param name="value_in"><see cref="TypedValue"/></param>
        /// <returns></returns>
        [DllImport(LIB)]
        static extern SUResult SUShadowInfoSetValue(IntPtr shadow_info, [MarshalAs(UnmanagedType.LPUTF8Str)] string key, IntPtr value_in);
        #endregion

        /// <summary>
        /// 属性个数
        /// </summary>
        public int Count
        {
            get
            {
                SketchUpExceptionHelper.ThrowSUResult(SUShadowInfoGetNumKeys(Id, out int c));
                return c;
            }
        }

        /// <summary>
        /// 所有属性
        /// </summary>
        public List<string> Keys
        {
            get
            {
                List<StringRef> keys = new List<StringRef>();
                for (int i = 0; i < Count; i++)
                {
                    keys.Add(new StringRef());
                }
                SketchUpExceptionHelper.ThrowSUResult(SUShadowInfoGetKeys(Id, keys.Count, keys.Select(k => k.Id).ToArray(), out _));
                return keys.Select(k => k.Dispose()).ToList();
            }
        }

        public TypedValue this[string key]
        {
            get
            {
                SketchUpExceptionHelper.ThrowSUResult(SUShadowInfoGetValue(Id, key, out IntPtr value));
                return new TypedValue(value);
            }
            set
            {
                SketchUpExceptionHelper.ThrowSUResult(SUShadowInfoSetValue(Id, key, value.Id));
            }
        }

        internal ShadowInfo(IntPtr id) => Id = id;
    }
}